package com.kiloai.base;

import android.app.Activity;
import android.app.Application;

import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

/**
 * Created by david on 2018/8/15.
 */

public class AppContext extends Application{

    private static AppContext instance;

    public AppContext() {
    }

    public static AppContext getInstance(){
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        OkGo.getInstance().init(this);
    }

    /**
     * 每次实例化activity都添加到这个容器中
     *
     * @param activity
     */
    private List<Activity> activities;

    public void addActivity(Activity activity) {
        if (activities == null) {
            activities = new ArrayList<>();
        }
        activities.add(activity);
    }

    /**
     * 每次finish activity 都从这个容器中移除
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activities == null) {
            return;
        }
        activities.remove(activity);
    }

    /**
     * finish all activity
     */
    public void finishAll() {
        if (activities == null) {
            return;
        }
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
    }
}
